package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/note/model"
)

type NoteStudyUserRecordsMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存用户课程学习记录
 */
func (mapper *NoteStudyUserRecordsMapper) SaveNoteStudyUserRecords(noteUserRecords *model.NoteStudyUserRecords) bool {
	_, err := orm.NewOrm().Insert(noteUserRecords)
	return err == nil
}

/**
 * 查询是否保存某节的记录
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *NoteStudyUserRecordsMapper) CountNoteStudyUserRecordsLesson(specialId uint64, userId uint64, noteId uint64, chapterId uint64, lessonId uint64, systemId uint64) int64 {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int64
	err := mysql.Raw("SELECT count(1) FROM xk_note_study_user_records WHERE special_id = ? and user_id = ? AND note_id = ? AND chapter_id = ? AND lesson_id = ? and system_id =?", specialId, userId, noteId, chapterId, lessonId, systemId).QueryRow(&total)
	if nil != err {
		return -1
	}
	return total
}

/**
 * 查询是否保存某节的记录
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *NoteStudyUserRecordsMapper) CountNoteStudyUserRecordsChapter(specialId uint64, userId uint64, noteId uint64, chapterId uint64, systemId uint64) int64 {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int64
	err := mysql.Raw("SELECT count(1) FROM xk_note_study_user_records WHERE special_id = ? and  user_id = ? AND note_id = ? AND chapter_id = ?  and system_id =? and lesson_id = 0", specialId, userId, noteId, chapterId, systemId).QueryRow(&total)
	if nil != err {
		return -1
	}
	return total
}

/**
 * 查询用户学习课程记录
 * @author feige
 * @date 2023-12-17
 * @version 1.0
 * @desc
 */
func (mapper *NoteStudyUserRecordsMapper) FindNoteStudyUserRecords(userId uint64, specialId uint64, systemId uint64) []*model.NoteStudyUserRecords {
	db := orm.NewOrm()
	var models []*model.NoteStudyUserRecords
	_, err := db.Raw("select * from xk_note_study_user_records  where user_id = ? and special_id = ? and system_id =? order by create_time desc", userId, specialId, systemId).QueryRows(&models)
	if nil != err {
		return nil
	}
	return models
}

/**
 * 查询课程和用户学习课程记录
 * @author feige
 * @date 2023-12-17
 * @version 1.0
 * @desc
 */
func (mapper *NoteStudyUserRecordsMapper) FindNoteStudyUserRecordsByUN(userId uint64, noteId uint64, systemId uint64) []*model.NoteStudyUserRecords {
	db := orm.NewOrm()
	var models []*model.NoteStudyUserRecords
	_, err := db.Raw("select * from xk_note_study_user_records  where user_id = ? and note_id = ? and system_id =? order by create_time desc", userId, noteId, systemId).QueryRows(&models)
	if nil != err {
		return nil
	}
	return models
}
